<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use app\common\model\Member as memberModel;
use app\model\home\Clock as clockModel;
use think\App;
use think\facade\Cache;
use \Throwable;

class Member extends BaseController
{
    protected $model;
    protected $clock;

    public function __construct(App $app, memberModel $model, clockModel $clock)
    {
        parent::__construct($app);
        $this->model = $model;
        $this->clock = $clock;
    }

    /**
     * @return mixed
     * 用户详情
     */
    public function info()
    {
        $this->params['member_id'] = $this->memberId;
        return $this->model->getInfo($this->params);
    }

    /**
     *
     * 切换身份
     */
    public function change()
    {
        try {
            validate(\app\validate\home\Member::class)->check($this->params);
            $identify = input('identify/d', 1);
            $identify == 1 && $value = 2;
            $identify == 2 && $value = 1;
            $this->params['member_id'] = $this->memberId;
            return $this->model->changeStatus($this->params, $value);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 一周业绩
     */
    public function week_order()
    {
        $reId = \app\model\home\Assistant::where('member_id', $this->memberId)->value('id');
        if (empty($reId)) {
            $listArr = [];
        } else {

            //总订单数
            $where = [];
            $where[] = ['re_id', '=', $reId];
            $where[] = ['pay_status', '=', 1];
            $where[] = ['order_type', '<>', 4];
            $listArr['order_num'] = \app\model\home\Order::where([$where])->count();
            $listArr['order_money'] = \app\model\home\Order::where([$where])->sum('pay_price') ?: 0;
            $listArr['tc_money'] = $listArr['order_money'] * 0.2;
        }
        return $this->returnRight($listArr);
    }

    /**
     * @return array
     * 打卡
     */
    public function clock()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->clock->add($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 更换头像
     */
    public function change_avatar()
    {
        try {
            $this->params['member_id'] = $this->memberId;
            return $this->model->update_avatar($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }

    /**
     * @return array
     * 绑定手机
     */
    public function bindPhone()
    {
        try {
            $smsCode = Cache::get("sms:mobile:{$this->params['phone']}");
            $code = $this->params['code'];
            if ($smsCode != $code) {
                return $this->returnError(43);
            }
            $this->params['member_id'] = $this->memberId;
            return $this->model->bind($this->params);
        } catch (\Exception $ex) {
            return $this->returnException($ex);
        }
    }
}
